`
https://leetcode.cn/problems/product-of-the-last-k-numbers/
`


var ProductOfNumbers = function () {
  this.prefix = [1]
};

/** 
* @param {number} num
* @return {void}
*/
ProductOfNumbers.prototype.add = function (num) {
  if (num === 0) {
    // 新添加进来的数字为 0 ，意味着在获取这个元素之前的所有结果都是 0
    // 也意味着前面的记录都没意义了
    this.prefix = [1]
    return
  }
  const n = this.prefix.length
  // 计算新添加进来的数字的前缀积计算结果
  this.prefix.push(this.prefix[n - 1] * num)
};

/** 
* @param {number} k
* @return {number}
*/
ProductOfNumbers.prototype.getProduct = function (k) {
  const n = this.prefix.length

  // k 大于当前前缀和数组的最高索引，说明访问到了有 0 的区间
  if (k > n - 1) return 0

  return this.prefix[n - 1] / this.prefix[n - 1 - k]
};

/** 
* Your ProductOfNumbers object will be instantiated and called as such:
* var obj = new ProductOfNumbers()
* obj.add(num)
* var param_2 = obj.getProduct(k)
*/